import axios from 'axios'
import store from '../store'
let access_token=(function(){
    return localStorage.getItem("access_token");
})();
//请求拦截 设置统一的header
axios.interceptors.request.use(
    config =>{
        // 当有发起请求的时候设置store里面的loading属性为true
        store.dispatch("setLoading",true);
        if (localStorage.jwtToken) {
            config.headers.Authorization = localStorage.jwtToken;
            config.url = config.url + "access_token";
        }
        // if (config.url.indexOf(url) === -1) {
        //     config.url = config.url + access_token;/*拼接完整请求路径*/
        // }
        return config
    },error =>{
        return Promise.reject(error);
    });
//响应拦截 
axios.interceptors.response.use(
    response =>{
        // 响应的时候设置store里面的loading属性为false
        store.dispatch("setLoading",false);
        return response
    },
    error =>{
        store.dispatch("setLoading",false);
        return Promise.reject(error)
    });
export default axios;